//
//  StatusCell.swift
//  DSWeibo
//
//  Created by J on 16/4/24.
//  Copyright © 2016年 J. All rights reserved.
//

import UIKit
import SDWebImage.UIImageView_WebCache
private let pictureViewCellID = "pictureViewCell"


//枚举中可以定义方法   保存cell的重用ID
enum StatusTableViewCellIDentifier: String{
     case NorMalCell = "NorMalCase"//原创微博id
     case retweetCell = "retweetMalCase"//转发微博id
     
     
     //如果在枚举中利用static修饰一个方法 相当于类中static修饰方法  +方法
     //如果调用枚举之rawValue， 那么意味着拿到枚举对应的原始值
     static func cellID(Status: Statuses) -> String{
          
          return Status.retweeted_status != nil ? retweetCell.rawValue : NorMalCell.rawValue//.rawValue 枚举原始值
     }

}




class StatusTableViewCell: UITableViewCell {
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        //初始化UI
        setupUI()

        
    }
    


    var  status : Statuses?{
        didSet{
          
          topView.status = status//顶部视图  名字时间来源
          contentLabel.text = status?.text//微博文字内容

            //设置配图尺寸
            //1.1根据模型设置配图尺寸
               pictureView.status = status//设置图片数据
          if ((status?.retweeted_status?.storePicURLS) != nil) {
               pictureView.status = status?.retweeted_status//设置图片数据
               self.layoutIfNeeded()
          }
          let size = pictureView.calculateImageSize()

            //1.2设置配图尺寸
            pictureView.snp_updateConstraints { (make) in
                make.size.equalTo(size.viewSize)
            }
          
          pictureViewTopPadding = (size == (CGSizeZero,CGSizeZero) ? 0 : 5)
          pictureView.backgroundColor = self.contentView.backgroundColor
          //1.4刷表
           self.pictureView.reloadData()
        }
    }
     
     
     //获取行高
     func rowHeight(status:Statuses) -> (CGFloat){
          //1.
          self.status = status.retweeted_status != nil ? status.retweeted_status:status//调用didSet计算配图高度
          //2. 更新整个cell
          self.layoutIfNeeded()
          //3.返回底部视图最大Y
          return CGRectGetMaxY(footerView.frame)
     }
     

     
     
     
     
    
     func setupUI(){
          
          let superView = contentView

        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(footerView)
     
        topView.snp_makeConstraints { (make) in
          make.top.equalTo(superView)
          make.left.equalTo(superView)
          make.width.equalTo(UIScreen.mainScreen().bounds.width)
          make.height.equalTo(100)
     }
     
     
        contentLabel.snp_makeConstraints { (make) in
            make.left.equalTo(topView).offset(10)
            make.top.equalTo(topView.snp_bottom)
        }

          
        footerView.snp_makeConstraints { (make) in
            make.left.equalTo(superView)
            make.right.equalTo(superView)
            make.height.equalTo(44)
        }

        
    }
    
     
     var pictureViewTopPadding = 0
     
      lazy var topView: StatusTableViewTopView =  StatusTableViewTopView()


    ///正文
     lazy var contentLabel : UILabel = {
        let label = UILabel.createLabel(UIColor.lightGrayColor(), font:15 )
        label.numberOfLines = 0
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.size.width - 40
        return label
    }()
    
    private lazy var layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    //配图
      lazy var pictureView : StatusTableViewPictureView = StatusTableViewPictureView()//self.
    
    ///底部工具条
    
    
     lazy var footerView : StatusTableViewBottomView = StatusTableViewBottomView()

    
     
     
     required init?(coder aDecoder: NSCoder) {
          fatalError("init(coder:) has not been implemented")
     }
}


